$(function() {
	var setting = {
	    	async: {
	    		type: 'post',
	    		dataType:'json',
	    		dataFilter: dataFilter ,
	    		autoParam:["permId"],
	    		url: ctx + '/admin/admin/permissions/tree',
	    		enable: true
	    	},
	    	view: {
	    		dblClickExpand: true,
	    		showLine: false,
	    		selectedMulti: true,
	    		expandSpeed:"",
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom
	    	},
	    	edit: {
				enable: false
			},
	    	data: {
	    		key:  {
	    			checked: 'checked',
	    			children: 'children' ,
	    			name: 'permName' ,
	    			title:'description' ,
	    			url: ''	
	    		},
	    		simpleData: {
	    			enable:true,
	    			idKey: "permId",
	    			pIdKey: "parentPermId",
	    			rootPId: ""
	    		}
	    	},
	    	callback: {
	    		beforeClick: beforeClick
	    	}
	    };

	    function dataFilter (treeId, parentNode, data){
	    	console.debug('dataFilter is  ' , treeId, parentNode, data) ;
	    	
	    	if (!data || data.code != 1) return null;
	    	var childNodes = data.data ;
	    	for (var i=0, l=childNodes.length; i<l; i++) {
	    		childNodes[i].isParent = true;
	    	}
	    	
	    	return childNodes;
	    }
	    function beforeClick(treeId, treeNode) {
	    	/* if (!treeNode.isParent) {
	    		alert("请选择父节点");
	    		return false;
	    	} else {
	    		return true;
	    	} */
	    	seclectNode = treeNode ;
		    	return true;
	    }

	    function refreshNode(treeId, treeNode) {
	    	var zTree = $.fn.zTree.getZTreeObj(tree) ;
	    	var type = "refresh" ;
	    	var silent = true ;
	    	if (!treeNode) {
	    		var msg = "<h4><span class='danger'>要选择一个节点才能刷新哦!</span></h4>" ;
				$('#alertModal .modal-body').html(msg) ;
				$('#alertModal').modal({
					backdrop: 'static' ,
					show: true
				}) ;
				return false ;
	    	}
			zTree.reAsyncChildNodes(treeNode, type, silent);
//			if (!silent) zTree.selectNode(treeNode);
	    }
	    
	    function addHoverDom(treeId, treeNode) {
	    	console.debug('addHoverDom is  ' , treeId) ;
	    	var treeObj = $.fn.zTree.getZTreeObj(tree);
			var sObj = $("#" + treeNode.tId + "_span");
			
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
				+ "' title='添加节点' onfocus='this.blur();'></span>";
			var editStr = "<span class='button edit' id='editBtn_" + treeNode.tId
			+ "' title='编辑节点' onfocus='this.blur();'></span>";
			var removeStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
			+ "' title='删除节点' onfocus='this.blur();'></span>";
			
			var refreshStr = "<span class='button refresh' id='refreshBtn_" + treeNode.tId
			+ "' title='刷新节点' onfocus='this.blur();'></span>";
			
			sObj.after(refreshStr);
			sObj.after(removeStr);
			sObj.after(editStr);
			sObj.after(addStr);
			
			var addBtn = $("#addBtn_"+treeNode.tId);
			/**添加节点*/
			if (addBtn) addBtn.bind("click", function(){
				treeObj.selectNode(treeNode);
				$('#layoutModal').modal({
					backdrop: 'static' ,
					remote: ctx+'/admin/admin/permissions/create?parentPermId='+treeNode.permId,
					show: true
				}) ;
				return false;
			});
			
			var editBtn = $("#editBtn_"+treeNode.tId);
			/**编辑节点*/
			if (editBtn) editBtn.bind("click", function(){
				var parentNode = treeNode.getParentNode() ;
				if(parentNode){
					treeObj.selectNode(parentNode);
				}
		    	$('#layoutModal').modal({
					backdrop: 'static' ,
					remote: ctx+'/admin/admin/permissions/modify/' + treeNode.permId ,
					show: true
				}) ;
				return false;
			});
			
			var removeBtn = $("#removeBtn_"+treeNode.tId);
			/**删除节点*/
			if (removeBtn) removeBtn.bind("click", function(){
				var parentNode = treeNode.getParentNode() ;
				if(parentNode){
					treeObj.selectNode(parentNode);
				}
				removeOrg(treeId, treeNode) ;
				return false;
			});
			
			var refreshBtn = $("#refreshBtn_"+treeNode.tId);
			/**刷新节点*/
			if (refreshBtn) refreshBtn.bind("click", function(){
				refreshNode(treeId, treeNode) ;
				return false;
			});
		};
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.tId).unbind().remove();
			$("#editBtn_"+treeNode.tId).unbind().remove();
			$("#removeBtn_"+treeNode.tId).unbind().remove();
			$("#refreshBtn_"+treeNode.tId).unbind().remove();
		};
	    
		/**删除某个权限*/
		function removeOrg(treeId, treeNode){
			var parentNode = treeNode.getParentNode() ;
			msg = "<h4><span class='danger'>改操作无法恢复哦?</span></h4>" ;
			$('#dialogModal .modal-body').html(msg) ;
			$('#dialogModal').modal({
				backdrop: 'static' ,
				show: true
			}) ;
			$('#dialogBtn').click(function(){
				XHZ.post(ctx + "/admin/admin/permissions/delete",{id: treeNode.permId}, function(data){
					var msg = "操作成功" ;
					$('#dialogModal').modal('hide') ;
					if(data.code == 1){
						if(parentNode){
							var type = "refresh" ;
					    	var silent = true ;
					    	var zTree = $.fn.zTree.getZTreeObj(tree) ;
					    	zTree.reAsyncChildNodes(parentNode, type, silent);
						}
					}else{
						msg = data.message ;
					}
					$('#alertModal .modal-body').html(msg) ;
					$('#alertModal').modal({
						backdrop: 'static' ,
						show: true
					}) ;
				}) ;
			}) ;
		}

	    /*************初始化*********************************/
	    
	    $.fn.zTree.init($("#" + tree), setting);
		
});

